package io.mft.runtime.function.process;

import io.mft.api.domain.ExecutingResource;
import io.mft.api.domain.Regulation;
import io.mft.commons.function.ExecuteReturn;
import io.mft.plugin.PluginContext;
import io.mft.plugin.PluginFunctionExecutor;
import io.mft.plugin.PluginInvoker;
import io.mft.plugin.PluginRequest;
import io.mft.plugin.PluginResponse;
import io.mft.runtime.function.ApiContext;
import io.mft.runtime.function.FunctionExecutor;

public abstract class ApiFunctionProcessExecutor<T extends Regulation> extends FunctionExecutor {
	private Regulation regulation;
	
	@SuppressWarnings("unchecked")
	public T getRegulation() {
		return (T)(regulation);
	}
	public void setRegulation(Regulation regulation) {
		this.regulation = regulation;
	}
	
	protected final <R extends Regulation> ExecuteReturn executePluginFunction(ApiContext context,R regulation,String pluginId,String funcNo) {
		ExecutingResource<R> executingResource=new ExecutingResource<R>();
		executingResource.setApi(context.getApiExecutorObject().getApi());
		executingResource.setExecutingRegulation(regulation);
		PluginFunctionExecutor<ExecutingResource<R>> executor=PluginInvoker.instancePluginFunctionExecutor(funcNo);
		PluginRequest pluginRequest=new PluginRequest(context.getRequest().getRequestValue());
		
		PluginResponse pluginResponse=new PluginResponse(context.getResponse());
		pluginResponse.setDsKeyNamespace(regulation.uid());
		PluginContext<ExecutingResource<R>> pluginContext=new PluginContext<ExecutingResource<R>>(pluginRequest, executingResource,pluginResponse);
		ExecuteReturn executeReturn=executor.doExecute(pluginContext);
		return executeReturn;
	}
}
